<?php
class Shipping_Model_ShipTypeCountryMap
{
    private $_table;
	public function __construct(){
		$this->_table = new Shipping_Model_DbTable_ShipTypeCountryMap();
	}


	public function add($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='stcm_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}


	public function delete($value,$field='stcm_id'){

		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);

		return $this->_table->delete($where);

	}


	public function getByValue($value,$field='stcm_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}

    public function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition["country_id"]) && $condition["country_id"]!=''){
            $select->where("country_id =?",$condition["country_id"]);
        }
		if(isset($condition["st_code"]) && $condition["st_code"]!=''){
            $select->where("st_code =?",$condition["st_code"]);
        }
        if(isset($condition["stcm_area"]) && $condition["stcm_area"]!=''){
            $select->where("stcm_area =?",$condition["stcm_area"]);
        }


        if(isset($condition["stcm_id"]) && $condition["stcm_id"]!=''){
            $select->where("stcm_id <>?",$condition["stcm_id"]);
        }

        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchone($select);
        }else{
            if($orderby!='')
            {
                $orderbyArr= explode(',',$orderby);
                foreach($orderbyArr as $order)
                {
                    $select->order($order);
                }

            }
            if($pageSize>0 && $page>0)
            {

                $start = ($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
		    return $this->_table->getAdapter()->fetchAll($select);
        }

	}
	
	
		
	 public function getAvailShipping( $disRegion,$type='*'){
			$select = $this->_table->getAdapter()->select();
			$table  = $this->_table->info('name');
			$select->from($table,$type);
			$select->where("country_id =? ",$disRegion);
			$select->orWhere("country_id =? ",'0'); 
			$select->group('st_code');
			 
			return $this->_table->getAdapter()->fetchAll($select);
	 }
	 
	 public function getShippingArea( $disRegion,$shipCode,$type='*'){
			$select = $this->_table->getAdapter()->select();
			$table  = $this->_table->info('name');
			$select->from($table,$type);
			$select->where("country_id in (?)",array($disRegion,0));
		 
			$select->where("st_code =? ",$shipCode);
			 
			return $this->_table->getAdapter()->fetchAll($select);
	 }
	
	
}